package hot;

public class er {
    //必须要使用静态内部类才可以被实例化哦
     static class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }
    public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode h1=l1;
        ListNode h2=l2;
        //遍历l1这个链表
        while(h1!=null){
            //这个情况处理 l1大于l2的情况
            if(h2==null){
                break;
            }
            //这个情况处理l2大于l1的情况
            h1.val=h1.val+h2.val;
            if(h1.next==null&&h2.next!=null){
                h1.next=h2.next;
                break;
            }
            h2=h2.next;
            h1=h1.next;
        }
        return is_carry(l1);
    }
    public static ListNode is_carry(ListNode l1){
        ListNode h3=l1;
        while(h3!=null){
            if(h3.val>=10){
                if(h3.next==null){
                    ListNode h4=new ListNode(0);
                    h3.val=h3.val%10;
                    h3.next=h4;
                    h4.val+=1;
                    break;
                }
                h3.val=h3.val%10;
                h3.next.val+=1;
            }
            h3=h3.next;
        }
        return l1;
    }

    public static void main(String[] args) {
        ListNode h1=new ListNode();
        ListNode h2=new ListNode();
        ListNode h3=new ListNode();
        h1.val=2;
        h1.next=h2;
        h2.val=4;
        h2.next=h3;
        h3.val=3;
        ListNode h4=new ListNode();
        ListNode h5=new ListNode();
//        ListNode h6=new ListNode();
        h4.val=5;
        h4.next=h5;
        h5.val=6;
//        h5.next=h6;
//        h6.val=4;
        ListNode node = addTwoNumbers(h4, h1);
//        System.out.println(node);
    }
}
